﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Helpers;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Modules;
using Vit.Web.Helpers;
using Vit.Web.Providers;

namespace Vit.Web.Mvc.Controllers
{
    public class ModuleController : Vit.Web.Mvc.Controllers.ControllerBase
    {
        [HttpGet]
        public ActionResult Index(string id)
        {
            IModuleProvider mp = Unity.Current.Resolve<IModuleProvider>();
            ViewBag.InstalledModules = mp.GetModules();
            var modules = Unity.Current.ResolveAll<IModule>();
            return View(modules);
        }

        [HttpGet]
        public ActionResult Install(string id)
        {
            IModule module = Unity.Current.Resolve<IModule>(id.ToLower());
            IModuleProvider mp = Unity.Current.Resolve<IModuleProvider>();
            string msg = string.Empty;
            bool result = mp.InstallModule(module, out msg);
            ViewBag.Message = msg;
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult UnInstall(string id)
        {
            IModule module = Unity.Current.Resolve<IModule>(id.ToLower());
            IModuleProvider mp = Unity.Current.Resolve<IModuleProvider>();
            string msg = string.Empty;
            bool result = mp.UnInstallModule(module, out msg);
            ViewBag.Message = msg;
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Update(string id)
        {
            IModule module = Unity.Current.Resolve<IModule>(id.ToLower());
            IModuleProvider mp = Unity.Current.Resolve<IModuleProvider>();
            string msg = string.Empty;
            bool result = mp.UpdateModule(module, out msg);
            ViewBag.Message = msg;
            return RedirectToAction("Index");
        }
    }
}
